package com.xy.xylive.xyapi;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xylink.config.SDKConfigMgr;
import com.xylink.model.ConfLiveVideoRequest;
import com.xylink.model.ConferenceLiveVideos;
import com.xylink.model.LiveVideo;
import com.xylink.model.LiveVideoForConference;
import com.xylink.sdk.liveVideo.v3.LiveVideoApi;
import com.xylink.util.HttpUtil;
import com.xylink.util.Result;
import com.xylink.util.SignatureSample;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LiveVideoV3Api extends LiveVideoApi {
    private static SignatureSample signatureSample = new SignatureSample();
    private String getPrefixUrl(String enterpriseId) {
        return SDKConfigMgr.getServerHost(enterpriseId) + "/api/rest/external/v1/liveVideo3/enterprise/";
    }

    /**
     * 获取直播实际直播时长
     * @param enterpriseId
     * @param token
     * @param liveId
     * @return
     * @throws IOException
     */
    public Result<LiveStatRes> actualDuration(String enterpriseId, String token, String liveId) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "live/actualDuration?enterpriseId=" + enterpriseId + "&liveId=" + liveId;
        surl = signatureSample.appendSigniture("", "DELETE", token, surl);
        return HttpUtil.getResponse(surl, "GET", (String)null, LiveStatRes.class);
    }

    /**
     * 按直播id批量查询回放列表
     * @param enterpriseId
     * @param token
     * @param confLiveVideoRequest
     * @return
     * @throws IOException
     */
    @Deprecated
    public Result<ConferenceLiveVideos[]> getLiveVideoListById(String enterpriseId, String token,ConfLiveVideoRequest confLiveVideoRequest) throws IOException {
        String surl = this.getPrefixUrl(enterpriseId) + "liveVideoList?enterpriseId=" + enterpriseId;
        List<ConfLiveVideoRequest> confLiveVideoRequests = new ArrayList<>();
        confLiveVideoRequests.add(confLiveVideoRequest);
        String jsonEntity = (new ObjectMapper()).writeValueAsString(confLiveVideoRequests);
        surl = signatureSample.appendSigniture(jsonEntity, "PUT", token, surl);
        return HttpUtil.getResponse(surl, "PUT", jsonEntity, ConferenceLiveVideos[].class);
    }
}
